//$Id:Page.java 2010-1-4 下午02:38:24
package com.walllee.core.dao;

import java.util.List;
import com.walllee.core.util.PageUtil;

/**
 * 
 * 
 * @author Wall.Lee(xiaoshan2242@gmail.com)
 */
@SuppressWarnings("unchecked")
public class Page {
	public final static int DEFAULTPAGESIZE = 25;
	private int pageSize = DEFAULTPAGESIZE;
	private List items;
	private int totalCount;
	private int pageCount;
	private int startIndex = 0;
	private int previousPage = 0;
	private int nextPage = 0;
	private int currPage = 0;

	/**
	 * 分页信息的构造函数
	 * @param items				数据信息
	 * @param totalCount		总的记录数
	 */
	public Page(List items, int totalCount) {
		setPageSize(DEFAULTPAGESIZE);
		setTotalCount(totalCount);
		setItems(items);
		setStartIndex(0);
	}

	/**
	 *  分页信息的构造函数
	 * @param items				数据信息
	 * @param totalCount		总的记录数
	 * @param startIndex		开始显示记录的行号
	 */
	public Page(List items, int totalCount, int startIndex) {
		setPageSize(DEFAULTPAGESIZE);
		setTotalCount(totalCount);
		setCurrPage(PageUtil.index2Page(startIndex, DEFAULTPAGESIZE));
		setItems(items);
		setStartIndex(startIndex);
	}

	/**
	 * 分页信息的构造函数
	 * @param items				数据信息
	 * @param totalCount		总的记录数
	 * @param pageSize			每页记录数
	 * @param pages				当前页数
	 */
	public Page(List items, int totalCount, int pageSize, int pages) {
		setPageSize(pageSize);
		setCurrPage(pages);
		setTotalCount(totalCount);
		setItems(items);
		setPage(pages);
	}

	public List getItems() {
		return items;
	}

	public void setItems(List items) {
		this.items = items;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		if (totalCount > 0)
			this.totalCount = totalCount;
		int pages = totalCount / pageSize;
		if (totalCount % pageSize > 0)
			pages++;
		setPageCount(pages);
	}

	public int getStartIndex() {
		return startIndex;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		if (nextPage >= this.pageCount)
			this.nextPage = nextPage;
		else
			this.nextPage = nextPage + 1;
	}

	public int getPreviousPage() {
		return previousPage;
	}

	public void setPreviousPage(int previousPage) {
		if (previousPage < 1)
			this.previousPage = 1;
		else
			this.previousPage = previousPage - 1;
	}

	public void setPage(int curPage) {
		int startIndex = 0;
		startIndex = (curPage - 1) * pageSize;
		if (curPage < 1) {
			startIndex = 0;
		}
		if (curPage > pageCount)
			startIndex = (pageCount - 1) * pageSize;

		setStartIndex(startIndex);
		setNextPage(curPage);
		setPreviousPage(curPage);
		setCurrPage(curPage);
	}

	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}
}